import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/pages/nft_pages/pay_pages/add_bank_card_hf/add_bank_card_hf_controller.dart';
import 'package:yysk_app/pages/nft_pages/pay_pages/select_bank_list/select_bank_list_page.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/widget/button_widget.dart';
import 'package:yysk_app/widget/checkbox_widget.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';

class AddBankCardHfPage extends GetView<AddBankCardHfController> {
  static const String path = '/AddBankCardHfPage';

  const AddBankCardHfPage({super.key});

  @override
  Widget build(BuildContext context) {
    final double top = MediaQuery.of(context).padding.top;
    return Obx(
      () => Scaffold(
        body: SafeArea(top: false,child: Stack(
          children: [
            //添加背景色，代码如下：
            SizedBox(
              width: context.width,
              child: ImageAssetWidget(
                width: double.infinity,
                height: 173.px,
                imageUrl: 'static/images/wallet/top_bg.png',
              ),
            ),
            Column(
              children: [
                Expanded(
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(0, top, 0, 24.px),
                    child: Column(
                      children: [
                        CustomAppBar2(title: '添加支付储蓄卡', context: context),
                        Expanded(
                          child: SingleChildScrollView(
                            physics: AlwaysScrollableScrollPhysics(),
                            padding: EdgeInsets.all(21.px),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(height: 20.px),
                                _buildInput(
                                  '持卡人',
                                  controller.nameController,
                                  hint: '请输入姓名',
                                  context: context,
                                  maxLength: 18,
                                  addBankCardHfController: controller,
                                  disable: false,
                                ),
                                const SizedBox(height: 16),
                                _buildInput(
                                  '身份证',
                                  controller.idController,
                                  hint: '请输入身份证',
                                  context: context,
                                  maxLength: 18,
                                  addBankCardHfController: controller,
                                  disable: false,
                                ),
                                const SizedBox(height: 16),

                                Text(
                                  '证件期限类型',
                                  style: TextStyle(
                                    color: AppColors.fontColorBlack,
                                    fontSize: 16.px,
                                    fontWeight: FontWeight.bold,
                                    // fontFamily: 'CustomPINGFANG',
                                  ),
                                ),
                                _buildCartTypeTime(
                                  context: context,
                                  addBankCardHfController: controller,
                                ),
                                SizedBox(height: 16.px),
                                Text(
                                  '证件有效期',
                                  style: TextStyle(
                                    color: AppColors.fontColorBlack,
                                    fontSize: 16.px,
                                    fontWeight: FontWeight.bold,
                                    // fontFamily: 'CustomPINGFANG',
                                  ),
                                ),
                                _buildCartType(
                                  context: context,
                                  addBankCardHfController: controller,
                                ),
                                SizedBox(height: 16.px),
                                Text(
                                  '银行卡类型',
                                  style: TextStyle(
                                    color: AppColors.fontColorBlack,
                                    fontSize: 16.px,
                                    fontWeight: FontWeight.bold,
                                    // fontFamily: 'CustomPINGFANG',
                                  ),
                                ),
                                Container(
                                  width: context.width,
                                  height: 45.px,
                                  decoration: BoxDecoration(
                                    border: Border(
                                      bottom: BorderSide(
                                        width: 1.px,
                                        color: Color.fromRGBO(242, 242, 242, 1),
                                      ),
                                    ),
                                  ),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    crossAxisAlignment:
                                    CrossAxisAlignment.center,
                                    children: [
                                      Text(
                                        '借记卡',
                                        style: TextStyle(
                                          fontSize: 16.px,
                                          color: AppColors.fontColorBlack,
                                          fontFamily: 'CustomPINGFANG',
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                SizedBox(height: 16.px),
                                Text(
                                  '绑定银行卡',
                                  style: TextStyle(
                                    color: AppColors.fontColorBlack,
                                    fontSize: 16.px,
                                    fontWeight: FontWeight.bold,
                                    // fontFamily: 'CustomPINGFANG',
                                  ),
                                ),
                                GestureDetector(
                                  onTap: (){
                                    Get.toNamed('${SelectBankListPage.path}?type=add',arguments: {'type':'add'});
                                  },
                                  child:  Container(
                                    width: context.width,
                                    height: 45.px,
                                    decoration: BoxDecoration(
                                      border: Border(
                                        bottom: BorderSide(
                                          width: 1.px,
                                          color: Color.fromRGBO(242, 242, 242, 1),
                                        ),
                                      ),
                                    ),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment:
                                      CrossAxisAlignment.center,
                                      children: [
                                        controller.bankName.value==''? Text(
                                          '请选择银行',
                                          style: TextStyle(
                                            fontSize: 16.px,
                                            color: AppColors.fontColorGreenA9,
                                            fontFamily: 'CustomPINGFANG',
                                          ),
                                        ):Text(
                                          controller.bankName.value,
                                          style: TextStyle(
                                            fontSize: 16.px,
                                            color: AppColors.fontColorBlack,
                                            fontFamily: 'CustomPINGFANG',
                                          ),
                                        ),
                                        ImageAssetWidget(
                                          imageUrl:
                                          'static/images/mall/mine/right.png',
                                          width: 10.px,
                                          height: 10.px,
                                        ),
                                      ],
                                    ),
                                  ),

                                ),
                                SizedBox(height: 16.px),
                                _buildInput(
                                  '银行卡号',
                                  controller.cardNoController,
                                  hint: '持卡人本人银行卡号',

                                  addBankCardHfController: controller,
                                  context: context,
                                  maxLength: 19,
                                ),
                                SizedBox(height: 16.px),
                                _buildInput(
                                  '预留手机号',
                                  controller.phoneController,
                                  hint: '请输入银行卡预留手机号',

                                  addBankCardHfController: controller,
                                  context: context,
                                  maxLength: 11,
                                ),
                                SizedBox(height: 16.px),
                                controller.isBind.value == false
                                    ? Row(
                                  crossAxisAlignment:
                                  CrossAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      child: _buildInput(
                                        '验证码',
                                        controller.smsController,
                                        hint: '输入短信验证码',
                                        context: context,
                                        addBankCardHfController: controller,
                                        maxLength: 6,
                                      ),
                                    ),
                                    const SizedBox(width: 8),
                                    Obx(
                                          () => ElevatedButton(
                                        onPressed:
                                        controller.smsBtnText.value ==
                                            '获取验证码'
                                            ? controller.getbankCardId
                                            : null,
                                        style: ElevatedButton.styleFrom(
                                          backgroundColor: Colors.white,
                                          foregroundColor: Colors.white,
                                          elevation: 0,
                                          shape: RoundedRectangleBorder(
                                            borderRadius:
                                            BorderRadius.circular(16),
                                          ),
                                        ),
                                        child: Text(
                                          controller.smsBtnText.value,
                                          style: TextStyle(
                                            color: AppColors.fontColorBlack,
                                            fontSize: 16.px,
                                            fontWeight: FontWeight.normal,
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                )
                                    : Text(''),
                                const SizedBox(height: 34),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),

                SizedBox(height: 16.px),
                Obx(
                      () => ButtonWidget(
                    label: '确认绑定',
                    height: 54,
                    width: 322,
                    fontSize: 22.px,
                    imageBg: 'static/images/btm.png',
                    contentType: ContentType.other,
                    onPressed:
                    controller.agree.value && !controller.isLoading.value
                        ? controller.submit
                        : null,
                  ),
                ),
                SizedBox(height: 6.px),
              ],
            ),
          ],
        )),
      ),
    );
  }

  _buildCartType({
    required BuildContext context,
    required AddBankCardHfController addBankCardHfController,
  }) {
    return Container(
      width: context.width,
      height: 45.px,
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 1.px,
            color: Color.fromRGBO(242, 242, 242, 1),
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          controller.certBeginDate.value==''? GestureDetector(
            onTap: (){
              controller.pickValidityDate(0);
            },
            child: Text(
            '起始时间',
            style: TextStyle(
              fontSize: 16.px,
              color: AppColors.fontColorGreenA9,
              fontFamily: 'CustomPINGFANG',
            ),
          ),
          ): GestureDetector(
                  onTap: (){
                    
              controller.pickValidityDate(0);
            },
            child: Text(
            controller.certBeginDate.value,
            style: TextStyle(
              fontSize: 16.px,
              color: AppColors.fontColorBlack,
              fontFamily: 'CustomPINGFANG',
            ),
          ),
          ),
          Text(
            '   -    ',
            style: TextStyle(
              fontSize: 16.px,
              color: AppColors.fontColorGreenA9,
              fontFamily: 'CustomPINGFANG',
            ),
          ),
          controller.certEndDate.value==''?GestureDetector(
            onTap: (){
               if(controller.isLong.value==true){
                      return;
                    }
              controller.pickValidityDate(1);
            },
            child: Text(
            '截止日期',
            style: TextStyle(
              fontSize: 16.px,
              color: AppColors.fontColorGreenA9,
              fontFamily: 'CustomPINGFANG',
            ),
          ),
          ):GestureDetector(
               onTap: (){
                  if(controller.isLong.value==true){
                      return;
                    }
              controller.pickValidityDate(1);
            },
            child: Text(
            controller.certEndDate.value,
            style: TextStyle(
              fontSize: 16.px,
              color: AppColors.fontColorBlack,
              fontFamily: 'CustomPINGFANG',
            ),
          ),
          ),
        ],
      ),
    );
  }

  _buildCartTypeTime({
    required BuildContext context,
    required AddBankCardHfController addBankCardHfController,
  }) {
    return Obx(
      () => Container(
        width: context.width,
        height: 45.px,
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 1.px,
              color: Color.fromRGBO(242, 242, 242, 1),
            ),
          ),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              child: Row(
                children: [
                  CheckboxWidget(
                    checked: !controller.isLong.value,
                    size: 12.px,
                    onChanged: (value) {
                      controller.isLong.value = false;
                       controller.certEndDate.value = '';
                    },
                  ),
                  Text(
                    '非长期有效',
                    style: TextStyle(
                      fontSize: 16.px,
                      color: AppColors.fontColorBlack,
                      fontFamily: 'CustomPINGFANG',
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(width: 28.px),
            Container(
              child: Row(
                children: [
                  CheckboxWidget(
                    checked: controller.isLong.value,
                    size: 12.px,
                    onChanged: (value) {
                      consoleLog('${value}');
                      controller.isLong.value = true;
                       controller.certEndDate.value = '20990101';
                    },
                  ),
                  Text(
                    '长期有效',
                    style: TextStyle(
                      fontSize: 16.px,
                      color: AppColors.fontColorBlack,
                      fontFamily: 'CustomPINGFANG',
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildInput(
    String label,
    TextEditingController controller, {
    required BuildContext context,
    required int maxLength,
    required AddBankCardHfController addBankCardHfController,
    String? hint,
    bool? disable = true,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            color: AppColors.fontColorBlack,
            fontSize: 16.px,
            fontWeight: FontWeight.bold,
          ),
        ),
        TextField(
          enabled: disable,
          controller: controller,
          maxLength: maxLength,
          onChanged: (value) {
            // 使用Timer，2秒后执行
            // if (value.length >= 18) {
            //   Timer(const Duration(seconds: 1), () {
            //     print('2秒后执行');
            //     addBankCardHfController.isBindCard();
            //     // 在这里调用你想要延迟执行的方法
            //   });
            // }
          },
          decoration: InputDecoration(
            hintText: hint,
            border: InputBorder.none,
            counterText: '',
            hintStyle: TextStyle(color: AppColors.fontColorGrey),
          ),
          style: TextStyle(color: AppColors.fontColorBlack),
        ),
        Container(
          width: context.width,
          height: 1.px,
          color: AppColors.backgroundColorGreyF7,
        ),
      ],
    );
  }
}
